/**
 * jquery.gallery.js
 * http://www.codrops.com
 *
 * Copyright 2011, Pedro Botelho / Codrops
 * Free to use under the MIT license.
 *
 * Date: Mon Jan 30 2012
 */

(function($, undefined) {

	/*
	 * Gallery object.
	 */
	$.Gallery = function(options, element) {
		this.$el = $(element);
		this._init(options);

	};

	$.Gallery.defaults = {
		current : 0, // index of current item
		autoplay : false, // slideshow on / off
		interval : 2000  // time between transitions
	};
	$.Gallery.prototype = {
		_init : function(options) {
			this.options = $.extend(true, {}, $.Gallery.defaults, options);

			// support for 3d / 2d transforms and transitions
			this.support3d = Modernizr.csstransforms3d;
			this.support2d = Modernizr.csstransforms;
			this.supportTrans = Modernizr.csstransitions;

			this.$wrapper = this.$el.find('.dg-wrapper');

			this.$items = this.$wrapper.children();
			this.itemsCount = this.$items.length;

			this.$nav = this.$el.find('nav');
			this.$navPrev = this.$nav.find('.dg-prev');
			this.$navNext = this.$nav.find('.dg-next');
			// minimum of 3 items
			if(this.itemsCount < 3) {

				this.$nav.remove();
				return false;

			}

			this.current = this.options.current;

			this.isAnim = false;


			this._validate();

			this._layout();

			// load the events
			this._loadEvents();

			// slideshow
			if(this.options.autoplay) {

				this._startSlideshow();

			}

		},
		_validate : function() {

			if(this.options.current < 0 || this.options.current > this.itemsCount - 1) {

				this.current = 0;

			}

		},
		_layout : function() {
			// current, left and right items
			this._setItems();

			// current item is not changed
			// left and right one are rotated and translated
			var leftCSS, rightCSS, currentCSS;

			if(this.support3d && this.supportTrans) {
				leftCSS = 'gallery_left_3d';
				rightCSS = 'gallery_right_3d';
				currentCSS = 'gallery_center_3d';


			} else if(this.support2d && this.supportTrans) {
				leftCSS = 'gallery_left_2d';
				rightCSS ='gallery_right_2d';
				currentCSS = 'gallery_center_2d';
			}

			this.$leftItm.addClass(leftCSS)
			this.$rightItm.addClass(rightCSS)

			this.$currentItm.addClass(currentCSS);

		},
		_setItems : function() {

			this.$items.removeClass('gallery-display-center');

			this.$currentItm = this.$items.eq(this.current);
			this.$leftItm = (this.current === 0 ) ? this.$items.eq(this.itemsCount - 1) : this.$items.eq(this.current - 1);
			this.$rightItm = (this.current === this.itemsCount - 1 ) ? this.$items.eq(0) : this.$items.eq(this.current + 1);

			if(!this.support3d && this.support2d && this.supportTrans) {

				this.$items.css('z-index', 1);
				this.$currentItm.css('z-index', 999);

			}

			// next & previous items
			if(this.itemsCount > 3) {

				// next item
				this.$nextItm = (this.$rightItm.index() === this.itemsCount - 1 ) ? this.$items.eq(0) : this.$rightItm.next();
				this.$nextItm.css(this._getCoordinates('outright'));

				// previous item
				this.$prevItm = (this.$leftItm.index() === 0 ) ? this.$items.eq(this.itemsCount - 1) : this.$leftItm.prev();
				this.$prevItm.css(this._getCoordinates('outleft'));

			}

		},
		_loadEvents : function() {
			var _self = this;
			var _prevFun =  function(event) {

				if(_self.options.autoplay) {

					clearTimeout(_self.slideshow);
					_self.options.autoplay = false;

				}

				_self._navigate('prev');
				return false;

			};
			var _nextFun = function(event) {

				if(_self.options.autoplay) {

					clearTimeout(_self.slideshow);
					_self.options.autoplay = false;

				}

				_self._navigate('next');
				return false;

			};
			//由于3D原因 导致wrapper遮挡了left与right故需要注册空事件来进行事件穿透
			$(".dg-wrapper").on('click',function(){});
			this.$navPrev.on('click', _prevFun);
			$("figure[class*='gallery_left_']").live('click',_prevFun);
			this.$navNext.on('click', _nextFun);
			$("figure[class*='gallery_right_']").live('click',_nextFun);
			this.$wrapper.on('webkitTransitionEnd.gallery transitionend.gallery OTransitionEnd.gallery', function(event) {
				_self.$currentItm.addClass('gallery-display-center');
				_self.$items.removeClass('gallery-transition');
				_self.isAnim = false;

			});
		},
		_getCoordinates : function(position) {

			if(this.support3d && this.supportTrans) {

				switch( position ) {
					case 'outleft':
						return 'gallery_outleft_3d';
						break;
					case 'outright':
						return 'gallery_outright_3d';
						break;
					case 'left':
						return 'gallery_left_3d';
						break;
					case 'right':
						return 'gallery_right_3d';
						break;
					case 'center':
						return 'gallery_center_3d';
						break;
				};

			} else if(this.support2d && this.supportTrans) {

				switch( position ) {
					case 'outleft':
						return 'gallery_outleft_2d';
						break;
					case 'outright':
						return 'gallery_outright_2d';
						break;
					case 'left':
						return 'gallery_left_2d';
						break;
					case 'right':
						return 'gallery_right_2d';
						break;
					case 'center':
						return 'gallery_center_2d';
						break;
				};

			} else {
				switch( position ) {
					case 'outleft'	:
					case 'outright'	:
					case 'left'		:
					case 'right'	:
						return 'gallery_nocenter_garbage';
						break;
					case 'center'	:
						return 'gallery_center_garbage';
						break;
				};

			}

		},
		_navigate : function(dir) {

			if(this.supportTrans && this.isAnim)
				return false;

			this.isAnim = true;
			var animClass = 'gallery-transition';
			var baseClass = 'gallery_content gallery_shadow';
			switch( dir ) {

				case 'next' :

					this.current = this.$rightItm.index();

					// current item moves left
					this.$currentItm.removeClass().addClass(baseClass).addClass(this._getCoordinates('left')).addClass(animClass);

					// right item moves to the center
					this.$rightItm.removeClass().addClass(baseClass).addClass(this._getCoordinates('center')).addClass(animClass);

					// next item moves to the right
					if(this.$nextItm) {

						// left item moves out
						this.$leftItm.removeClass().addClass(baseClass).addClass(this._getCoordinates('outleft')).addClass(animClass);

						this.$nextItm.removeClass().addClass(baseClass).addClass(this._getCoordinates('right')).addClass(animClass);

					} else {

						// left item moves right
						this.$leftItm.removeClass().addClass(baseClass).addClass(this._getCoordinates('right')).addClass(animClass);

					}
					break;

				case 'prev' :

					this.current = this.$leftItm.index();

					// current item moves right
					this.$currentItm.removeClass().addClass(baseClass).addClass(this._getCoordinates('right')).addClass(animClass);

					// left item moves to the center
					this.$leftItm.removeClass().addClass(baseClass).addClass(this._getCoordinates('center')).addClass(animClass);

					// prev item moves to the left
					if(this.$prevItm) {

						// right item moves out
						this.$rightItm.removeClass().addClass(baseClass).addClass(this._getCoordinates('outright')).addClass(animClass);

						this.$prevItm.removeClass().addClass(baseClass).addClass(this._getCoordinates('left')).addClass(animClass);

					} else {

						// right item moves left
						this.$rightItm.removeClass().addClass(baseClass).addClass(this._getCoordinates('left')).addClass(animClass);

					}
					break;

			};

			this._setItems();

			if(!this.supportTrans){
				this.$currentItm.addClass('gallery-display-center');
			}

		},
		_startSlideshow : function() {

			var _self = this;

			this.slideshow = setTimeout(function() {

				_self._navigate('next');

				if(_self.options.autoplay) {

					_self._startSlideshow();

				}

			}, this.options.interval);

		},
		destroy : function() {

			this.$navPrev.off('.gallery');
			this.$navNext.off('.gallery');
			this.$wrapper.off('.gallery');

		}
	};

	var logError = function(message) {
		if(this.console) {
			console.error(message);
		}
	};

	$.fn.gallery = function(options) {

		if( typeof options === 'string') {

			var args = Array.prototype.slice.call(arguments, 1);

			this.each(function() {

				var instance = $.data(this, 'gallery');

				if(!instance) {
					logError("cannot call methods on gallery prior to initialization; " + "attempted to call method '" + options + "'");
					return;
				}

				if(!$.isFunction(instance[options]) || options.charAt(0) === "_") {
					logError("no such method '" + options + "' for gallery instance");
					return;
				}

				instance[options].apply(instance, args);

			});
		} else {
			this.each(function() {

				var instance = $.data(this, 'gallery');
				if(!instance) {
					$.data(this, 'gallery', new $.Gallery(options, this));
				}
			});
		}

		return this;

	};
})(jQuery);
